import requests
from bs4 import BeautifulSoup
def gethtml(url):
    try:
        re=requests.get(url,timeout=10)
        re.raise_for_status()
        re.encoding="utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""
def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    div_ls=soup.find_all("div",{"class":"list_content2"})
    li_ls=div_ls[0].find_all("li")
    all_ls=[]
    for li in li_ls:
        ls=[]
        span_ls=li.find("span")
        ls.append(span_ls.string)
        a_ls=li.find("a")
        ls.append(a_ls.attrs["title"])
        ls.append(a_ls.attrs["href"])
        all_ls.append(ls)
        ls=[]
    return all_ls
def writehtmldata(ls):
    with open(r"安徽水利水电职业技术学院.csv","w") as f:
        for line in ls:
            f.write(",".join(line)+"\n")
def main():
    url="https://www.ahsdxy.edu.cn/xwzx/syxw.htm"
    html=gethtml(url)
    ls=gethtmldata(html)
    writehtmldata(ls)

if __name__ == '__main__':
    main()